layui.use(['form', 'layer', 'jquery'], function () {
    $ = layui.jquery;
    let form = layui.form,
        layer = layui.layer;
    findById();

    function findById() {
        let id = sessionStorage.getItem("newsTypeId");
        //console.log(id)
        let res = myAjax("/back/news/type/id", {id: id}, "get");
        if (res != null) {
            if (res.data != null) {
                setProductData(res.data);
            }
        }
    }

    function setProductData(data) {
        //console.log(data);
        $("#name").val(data.name);
        $("#title").val(data.title);
        $(':radio[name="enable"][value="' + data.enable + '"]').prop("checked", true);
        $(':radio[name="isShow"][value="' + data.isShow + '"]').prop("checked", true);
    }

    //监听提交
    form.on('submit(edit)', function (data) {
        data = data.field;
        //console.log(data);
        data.id = sessionStorage.getItem("newsTypeId");
        //console.log(data);
        //发异步，把数据提交给java
        let res = myAjax("/back/news/type/update", data, "post");
        if (res != null) {
            if (res.count === 1) {
                layer.alert("修改成功", {
                        icon: 6
                    },
                    function () {
                        //关闭当前frame
                        xadmin.close();

                        // 可以对父窗口进行刷新
                        xadmin.father_reload();
                    });
            } else {
                layer.alert("修改失败");
            }
        }
        return false;
    });
});